package qjc.q_技巧;

public class q3_颜色分类 {
    public void sortColors(int[] nums) {
        int cur = 0;
        int left = 0;
        int n = nums.length;
        int right = n - 1;
        while (cur <= right) {
            if (nums[cur] == 0) {
                swap(nums, cur, left);
                cur++;
                left++;
            } else if (nums[cur] == 1) {
                cur++;
            } else {
                swap(nums, cur, right);
                right--;
            }
        }
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
